package pit.com.ordermobilesystem.a;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.adapter.OrderItemAdapter;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.model.OrderItem;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.tabhost.TabGroupActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SelectedActivity extends FragmentActivity {
	
	private ListView lv_danhsach = null;
	private TextView tv_tongcong;
	private ImageButton imbt_delete;
	private ImageView imv_trove;
	private ImageView imv_thanhtoan;

	private OrderItemAdapter adapter = null;
	public static int tien = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectedfragment);
		
		tv_tongcong = (TextView) findViewById(R.id.tv_tongcong);
		imbt_delete = (ImageButton) findViewById(R.id.imbt_delete);
		imv_trove = (ImageView) findViewById(R.id.iv_trove);
		imv_thanhtoan = (ImageView) findViewById(R.id.iv_thanhtoan);

		tien = 0;
		for (int j = 0; j < DetailsActivity.ListItem.size(); j++) {
			OrderItem newitem = new OrderItem();
			newitem = DetailsActivity.ListItem.get(j);
			int t = (int) newitem.getPrice();
			tien = tien + newitem.getNumberItem()*t;
		}
		
		lv_danhsach = (ListView) findViewById(R.id.lv_danhsach);
		adapter = new OrderItemAdapter(SelectedActivity.this, R.layout.seclectiem,
				DetailsActivity.ListItem);
		lv_danhsach.setAdapter(adapter);
		tv_tongcong.setText(String.valueOf(tien)+".000 đ");

		imbt_delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sulyxoa();
			}
		});

		imv_trove.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent previewMessage = new Intent();
				previewMessage.setClass(SelectedActivity.this, TitlesActivity.class);
	            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	            parentActivity.startChildActivity("TitlesActivity", previewMessage);
			}
		});
		imv_thanhtoan.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
								
				Intent previewMessage = new Intent();
				previewMessage.setClass(SelectedActivity.this, PaymentActivity.class);
	            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	            parentActivity.startChildActivity("PaymentActivity", previewMessage);
				
			}
		});
	}
	public void sulyxoa() {
		for (int i = lv_danhsach.getChildCount() - 1; i >= 0; i--) {

			View v = lv_danhsach.getChildAt(i);
			CheckBox chk = (CheckBox) v.findViewById(R.id.cb_click);
			// Nếu nó Checked thì xóa ra khỏi
			if (chk.isChecked()) {
				// xóa phần tử thứ i ra khỏi danh sách
				DetailsActivity.ListItem.remove(i);
			}

		}
		// sau khi xoa , phai tinh lai so tien cua Don hang
		tien = 0;
		for (int j = 0; j < DetailsActivity.ListItem.size(); j++) {
			OrderItem newitem = new OrderItem();
			newitem = DetailsActivity.ListItem.get(j);
			int t =(int) newitem.getPrice();
			tien = tien + newitem.getNumberItem()*t;
		}
		// Sau khi xóa xong thì gọi update giao diện
		adapter.notifyDataSetChanged();
		lv_danhsach.setAdapter(adapter);
		tv_tongcong.setText(String.valueOf(tien) + ".000đ");
	}
}
